using System.Collections.Generic;

namespace Mbs.ComponentModel
{
    class StackList<T> : IEnumerable<T>
    {
        private List<T> stack = null;
        private readonly int maxCapacity = 100;

        public StackList(int capacity)
        {
            maxCapacity = capacity;
            stack = new List<T>(maxCapacity);
        }

        public bool Contains(T item)
        {
            return stack.Contains(item);
        }

        public void Push(T item)
        {
            if (stack.Count >= maxCapacity - 1)
                stack.RemoveAt(0);
            stack.Add(item);
        }

        public T Pop()
        {
            T t = default(T);

            if (stack.Count >= 1)
            {
                t = stack[stack.Count - 1];
                stack.RemoveAt(stack.Count - 1);
            }
            return t;
        }

        public void MoveBack(T item)
        {
            if (CanMoveBack(item))
            {
                int index = stack.IndexOf(item);
                stack.Insert(++index, item);
            }
        }

        public void MoveForward(T item)
        {
            if (CanMoveForward(item))
            {
                int index = stack.IndexOf(item);
                stack.Insert(--index, item);
            }
        }

        public bool CanMoveBack(T item)
        {
            return stack.Contains(item)
                && stack.IndexOf(item) != stack.Count - 1;
        }
        public bool CanMoveForward(T item)
        {
            return stack.Contains(item)
               && stack.IndexOf(item) != 0;
        }


        public int Count
        {
            get { return stack.Count; }
        }

        public void Clear()
        {
            stack.Clear();
        }

        #region IEnumerable<TTarget> Members

        public IEnumerator<T> GetEnumerator()
        {
            return stack.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return stack.GetEnumerator();
        }

        #endregion
    }
}
